package com.liumuxian.exam.web.controller;

import com.liumuxian.exam.pojo.Article;
import com.liumuxian.exam.pojo.ArticleAttribute;
import com.liumuxian.exam.pojo.Handout;
import entity.ArticleVo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import util.IdWorker;
import util.POIUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class ArticleController {
    @Resource
    private POIUtil poiUtil;
    @Resource
    private IdWorker idWorker;

    @PostMapping("/save")
    public Result save(@RequestParam(value = "file", required = true) MultipartFile file){
        ArticleVo articleVo=poiUtil.world(file);
        Article article = new Article();
        article.setId(idWorker.nextId()+"");
        article.setName(articleVo.getName());
        List<ArticleAttribute> articleAttributeList = new ArrayList<>();
        List<Handout> handoutList = new ArrayList<>();

        for (int i=0; i<articleVo.getTitle().size();i++){
            ArticleAttribute articleAttribute = new ArticleAttribute();
            articleAttribute.setId(idWorker.nextId()+"");
            articleAttribute.setTitle(articleVo.getTitle().get(i));
            articleAttribute.setContent(articleVo.getContent().get((i+1)));
            articleAttribute.setArticleid(article.getId());
            String str = (String) articleVo.getHandout().get((i+1));
            String[] strs = str.split("\n");
            for (String s:strs){
                Handout handout = new Handout();
                handout.setId(idWorker.nextId()+"");
                handout.setArticleattributeid(articleAttribute.getId());
                if(s.startsWith("考点")){
                    handout.setType(0);
                    handout.setContent(s.substring(3));
                } else if(s.startsWith("考频")) {
                    handout.setType(1);
                    handout.setContent(s.substring(3));
                }else if(s.startsWith("知识点")) {
                    handout.setType(2);
                    handout.setContent(s.substring(4));
                }
                handoutList.add(handout);
            }
            articleAttributeList.add(articleAttribute);

        }
        return new Result(true, StatusCode.OK,"");
    }
    @PostMapping("/login")
    public Result login(@RequestBody String code){
        System.out.println(code);
        return new Result();
    }



}
